package com.example.helloandroid;

import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.webkit.ConsoleMessage;
import android.widget.TextView;

import android.location.*;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { 		
        super.onCreate(savedInstanceState);
Log.d("STAT", "Starting");
        
        //get location
        LocationManager myManager = (LocationManager)getSystemService(LOCATION_SERVICE);
        LocationListener ll = new mylocationlistener();
        myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
        Location fix= myManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        Display(fix);
		Report(fix);
    }
    
private void Display(Location fix){
	Log.d("STAT", "Starting Display");
   TextView tv = new TextView(this);
	try {
        setContentView(R.layout.main);
        
		String location = fix.toString();
		
		tv.setText(location);
		setContentView(tv);
	} catch (Exception e) {
		tv.setText("display error occured");
		Log.d("error", "display error");
		setContentView(tv);
		e.printStackTrace();
	}   
}
private static void Report(Location fix){
	Log.d("STAT", "reporting");
    try {
		URL u = new URL("ftp://android:Because8@173.83.202.172:21/android/locations.txt") ;
		URLConnection uc = u.openConnection() ;
		uc.setDoOutput(true);
//		InputStream in = uc.getInputStream();
//		InputStreamReader sr = new InputStreamReader(in);
		OutputStream out = uc.getOutputStream() ;
		OutputStreamWriter sw = new OutputStreamWriter(out);
		//out.write("THIS DATA WILL BE WRITTEN TO FILE".getBytes());
		
		BufferedWriter bw = new BufferedWriter(sw);
		
		long epoch = java.lang.System.currentTimeMillis();
		String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch));
		String locationString = ((String.valueOf(fix.getLatitude())+" "+(String.valueOf(fix.getLongitude() + " "+(String.valueOf(fix.getTime())+" "+ date +" " + (String.valueOf(java.lang.System.currentTimeMillis() )))))));
		//out.write(locationString.getBytes());
//		int i =0;
//		int count = 0;
//		while (i != -1){
//			count++;
//			i = sr.read();
//		}
		bw.append(locationString);
		bw.flush();
		bw.close();
	//	out.close();
	} catch (MalformedURLException e) {
	//	tv.setText("connection error occured");
	//	setContentView(tv);
		Log.d("error", "connection error");
		e.printStackTrace();
	} catch (IOException e) {
	//	tv.setText("error occured");
	//	setContentView(tv);
		Log.d("error", "connectionIO error");
		e.printStackTrace();
	}
}

    private class mylocationlistener implements LocationListener {
        
        public void onLocationChanged(Location location) {
        	Log.d("STAT", "listerner trigger");
            if (location != null) {
            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            Report(location);
            Display(location);
            //Toast.makeText();
            //    location.getLatitude() + "" + location.getLongitude(),
            //    Toast.LENGTH_LONG).show();
            }
        }
        public void onProviderDisabled(String provider) {
        }
      
        public void onProviderEnabled(String provider) {
        }
        
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        }
}

